package com.baiqian.doupo.gui;

import com.baiqian.doupo.Utils;
import com.baiqian.doupo.capability.DouPoModCapability;
import com.baiqian.doupo.capability.douqi.IDouQiCapability;
import com.baiqian.doupo.douji.DouJi;
import com.baiqian.doupo.gongfa.GongFa;
import com.baiqian.doupo.network.Networking;
import com.baiqian.doupo.network.SendPack;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.util.LazyOptional;

import java.util.ArrayList;

public class PlayerSkillGui extends Screen {
    private static final int IconMax = 32;
    private static final ResourceLocation PlayerSkillGuiBgImg = new ResourceLocation(Utils.MOD_ID, "textures/gui/player_skill_gui.png");
    private static final ResourceLocation PlayerSkillGuiDouJiChooseImg = new ResourceLocation(Utils.MOD_ID, "textures/douji/douji_choose.png");
    private static final ResourceLocation PlayerSkillGuiGongFaChooseImg = new ResourceLocation(Utils.MOD_ID, "textures/gongfa/gongfa_choose.png");
    private static ResourceLocation PlayerSkillGuiGongFa;
    private static ResourceLocation[] PlayerSkillGuiDouJi;
    private static ResourceLocation[] PlayerSkillGuiGongFaList;
    private static ResourceLocation[] PlayerSkillGuiDouJiList;

    private TranslationTextComponent content;
    private LazyOptional<IDouQiCapability> douqiCap = null;
    private int gongfa_choose_gui=0;//是否选择了功法；0不是，1是
    private int douji_choose_gui=0;//是否选择了斗技；0不是，其余分别对应1~4
    private ArrayList<GongFa> gongfas=null;
    private ArrayList<DouJi> doujis=null;
    private int gongfa_choose;//选择的功法
    private int douji_group_choose;//选了的是第几组斗技，0是第一组
    private int[] douji_group;//共16格，4组斗技
    private int gongfa_index;//功法基址
    private int douji_index;//斗技基址
    private int gongfa_max;//当前显示的功法上限
    private int douji_max;//当前显示的斗技上限
    private int width;
    private int height;

    private void loadDoujiIcon(){
        for(int i=0;i<4;++i)
            if(douji_group[douji_group_choose*4+i]!=-1)
                PlayerSkillGuiDouJi[i]=new ResourceLocation(Utils.MOD_ID, "textures/douji/douji_"+doujis.get(douji_group[douji_group_choose*4+i]).douji_icon+".png");
    }
    private void loadGongFaIcon(){
        if(gongfa_choose!=-1)
            PlayerSkillGuiGongFa = new ResourceLocation(Utils.MOD_ID, "textures/gongfa/gongfa_"+gongfas.get(gongfa_choose).gongfa_icon+".png");
    }
    private void loadDoujiListIcon(){
        int max = doujis.size()-douji_index*IconMax;
        max = Math.min(max, IconMax);
        douji_max=max;
        for(int i=0;i<max;++i)
            PlayerSkillGuiDouJiList[i] = new ResourceLocation(Utils.MOD_ID, "textures/douji/douji_" + doujis.get(douji_index * IconMax + i).douji_icon + ".png");
    }
    private void loadGongFaListIcon(){
        int max = gongfas.size()-gongfa_index*IconMax;
        max = Math.min(max, IconMax);
        gongfa_max=max;
        for(int i=0;i<max;++i)
            PlayerSkillGuiGongFaList[i]=new ResourceLocation(Utils.MOD_ID, "textures/gongfa/gongfa_"+gongfas.get(gongfa_index*IconMax+i).gongfa_icon+".png");
    }
    private void showGongFa(MatrixStack matrixStack,GongFa gongFa){
        String str= gongFa.gongfa_name+"·";
        str+=new TranslationTextComponent("gui."+Utils.MOD_ID+".book_level_big_"+gongFa.gongfa_level_big).getString();
        str+=new TranslationTextComponent("gui."+Utils.MOD_ID+".book_level_middle_"+gongFa.gongfa_level_middle).getString();
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_gongfa_property").getString();
        str+= new TranslationTextComponent("gui."+Utils.MOD_ID+".soul_"+gongFa.gongfa_property).getString();
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172+20) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_gongfa_douqi_speed").getString();
        str+=gongFa.gongfa_douqi_speed;
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172+20*2) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_gongfa_compress_speed").getString();
        str+=gongFa.gongfa_compress_speed;
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172+20*3) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_gongfa_soul_speed").getString();
        str+=gongFa.gongfa_soul_speed;
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172+20*4) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_gongfa_attack_num").getString();
        str+=gongFa.gongfa_attack_num;
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172+20*5) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_gongfa_defense_num").getString();
        str+=gongFa.gongfa_defense_num;
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172+20*6) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_gongfa_recover_num").getString();
        str+=gongFa.gongfa_recover_num;
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+122) / 2, (height+172+20*7) / 2, 0x000000);
    }
    private void showDouJi(MatrixStack matrixStack,DouJi douJi){
        int str_num=1;
        String str= douJi.douji_name+"·";
        str+=new TranslationTextComponent("gui."+Utils.MOD_ID+".book_level_big_"+douJi.douji_level_big).getString();
        str+=new TranslationTextComponent("gui."+Utils.MOD_ID+".book_level_middle_"+douJi.douji_level_middle).getString();
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+172) / 2, 0x000000);
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_douji_property").getString();
        str+= new TranslationTextComponent("gui."+Utils.MOD_ID+".soul_"+douJi.douji_property).getString();
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+192) / 2, 0x000000);
        str="持续时间:";
        if(douJi.douji_keep_time==-1)
            str+="永续";
        else if(douJi.douji_keep_time<20)
            str+="瞬发";
        else
            str+="("+ (douJi.douji_keep_time/20) +"s)";
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+212) / 2, 0x000000);
        if(douJi.douji_attack_num>0.0){
            str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_douji_attack").getString();
            str+= douJi.douji_attack_num;
            drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+212+20*str_num) / 2, 0x000000);
            ++str_num;
        }
        if(douJi.douji_defense_num>0.0){
            str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_douji_defense").getString();
            str+= douJi.douji_defense_num;
            drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+212+20*str_num) / 2, 0x000000);
            ++str_num;
        }
        if(douJi.douji_recover_num>0.0){
            str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_douji_recover").getString();
            str+= douJi.douji_recover_num;
            drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+212+20*str_num) / 2, 0x000000);
            ++str_num;
        }
        if(douJi.douji_use_num>1){
            str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_douji_use_num").getString();
            str+= douJi.douji_use_num;
            drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+212+20*str_num) / 2, 0x000000);
            ++str_num;
        }
        str = new TranslationTextComponent("gui."+Utils.MOD_ID+".book_douji_use_rebirth_time").getString();
        if(douJi.douji_use_rebirth_time<20)
            str+= "瞬间";
            else
            str+= (douJi.douji_use_rebirth_time/20) +"s";
        drawString(matrixStack, this.minecraft.fontRenderer, str, (width+428) / 2, (height+212+20*str_num) / 2, 0x000000);
    }

    public PlayerSkillGui(ITextComponent titleIn) {
        super(titleIn);
        PlayerEntity player = Minecraft.getInstance().player;
        if (player != null) {
            douqiCap = player.getCapability(DouPoModCapability.DOUQI_CAPABILTTY);
            if (douqiCap.isPresent()) {
                douqiCap.ifPresent((l) -> {
                            douji_max=0;
                            gongfa_max=0;
                            gongfa_index=0;
                            douji_index=0;
                            gongfas=l.getGongFas();
                            doujis=l.getDouJis();
                            gongfa_choose=l.getGongFaChoose();
                            douji_group_choose=l.getDouJiGroupChoose();
                            douji_group=l.getDouJiGroup();
                            PlayerSkillGuiDouJi=new ResourceLocation[4];
                            PlayerSkillGuiGongFaList=new ResourceLocation[IconMax];
                            PlayerSkillGuiDouJiList=new ResourceLocation[IconMax];
                            loadGongFaIcon();
                            loadDoujiIcon();
                            loadDoujiListIcon();
                            loadGongFaListIcon();
                        }
                );
            }
        }
    }

    @Override
    protected void init() {
//        this.minecraft.keyboardListener.enableRepeatEvents(true);
        super.init();
        width = (this.minecraft.currentScreen.width - 630 / 2);//112
        height = (this.minecraft.currentScreen.height - 380 / 2);//50
        Button douji_group_choose_button_left = new Button((width+322) / 2, (height+110) / 2, 34 / 2, 34 / 2,
                new TranslationTextComponent("<"), (button) -> {
            if (douji_group_choose > 0) {
                --douji_group_choose;
                loadDoujiIcon();
            }
        });
        Button douji_group_choose_button_right = new Button((width+578) / 2, (height+110) / 2, 34 / 2, 34 / 2,
                new TranslationTextComponent(">"), (button) -> {
            if (douji_group_choose < 3) {
                ++douji_group_choose;
                loadDoujiIcon();
            }
        });
        Button douji_group_button_left = new Button((width+348) / 2, (height+330) / 2, 20 / 2, 20 / 2,
                new TranslationTextComponent("<"), (button) -> {
            if(douji_index>0){
                --douji_index;
                loadDoujiListIcon();
            }
        });
        Button douji_group_button_right = new Button((width+394) / 2, (height+330) / 2, 20 / 2, 20 / 2,
                new TranslationTextComponent(">"), (button) -> {
            if((douji_index+1)*IconMax<doujis.size()){
                ++douji_index;
                loadDoujiListIcon();
            }
        });
        Button gonfa_button_left = new Button((width+42) / 2, (height+330) / 2, 20 / 2, 20 / 2,
                new TranslationTextComponent("<"), (button) -> {
            if(gongfa_index>0) {
                --gongfa_index;
                loadGongFaListIcon();
            }
        });
        Button gonfa_button_right = new Button((width+86) / 2, (height+330) / 2, 20 / 2, 20 / 2,
                new TranslationTextComponent(">"), (button) -> {
            if((gongfa_index+1)*IconMax<gongfas.size()){
                ++gongfa_index;
                loadGongFaListIcon();
            }
        });
        this.addButton(douji_group_choose_button_left);
        this.addButton(douji_group_choose_button_right);
        this.addButton(douji_group_button_left);
        this.addButton(douji_group_button_right);
        this.addButton(gonfa_button_left);
        this.addButton(gonfa_button_right);
    }

    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        double mouse_x= mouseX*2;
        double mouse_y= mouseY*2;
        if(mouse_x>(width+32)&&mouse_x<(width+32)+52&&mouse_y>(height+102)&&mouse_y<(height+102)+48) {
            if(gongfa_choose_gui==1){
                gongfa_choose_gui=0;
            }else {
                gongfa_choose=-1;//把原来的功法去了
                gongfa_choose_gui = 1;
            }
            douji_choose_gui=0;
            return super.mouseClicked(mouseX, mouseY, button);
        }
        for(int i=0;i<4;++i) {//如果有斗技，展示四个斗技
            if (mouse_x>(width+352+54*i)&&mouse_x<(width+352+54*i+48)&&mouse_y>height+104&&mouse_y<height+104+44) {
                if(douji_choose_gui==i+1) {
                    douji_choose_gui = 0;
                }
                else {
                    douji_group[douji_group_choose*4+i]=-1;//把斗技去了
                    douji_choose_gui = i + 1;
                }
                gongfa_choose_gui=0;
                return super.mouseClicked(mouseX, mouseY, button);
            }
        }
        int x,y,now=0;
        for(y=0;y<8&&now<gongfa_max;++y){
            for(x=0;x<4&&now<gongfa_max;++x){
                if(mouse_x>(width+36+x*20)&&mouse_x<(width+36+x*20)+16&&mouse_y>(height+172+y*20)&&mouse_y<(height+172+y*20)+16) {
                    if(gongfa_choose_gui==1){//开始装备功法
                        gongfa_choose=gongfa_index*IconMax+now;
                        if (douqiCap.isPresent()) {
                            douqiCap.ifPresent((l) -> {
                                Networking.INSTANCE.sendToServer(new SendPack(6,"gongfa",gongfa_choose));
                                    }
                            );
                        }
                        gongfa_choose_gui=0;
                        loadGongFaIcon();
                        return super.mouseClicked(mouseX, mouseY, button);
                    }
                }
                ++now;
            }
        }
        now=0;
        for(y=0;y<8&&now<douji_max;++y){
            for(x=0;x<4&&now<douji_max;++x){
                if(mouse_x>(width+342+x*20)&&mouse_x<(width+342+x*20)+16&&mouse_y>(height+172+y*20)&&mouse_y<(height+172+y*20)+16) {
                    if (douji_choose_gui != 0) {// 开始装备斗技
                        douji_group[douji_group_choose*4+douji_choose_gui-1]=douji_index*IconMax+now;
                        if (douqiCap.isPresent()) {
                            douqiCap.ifPresent((l) -> {
                                        Networking.INSTANCE.sendToServer(new SendPack(6,"douji",douji_group));
                                    }
                            );
                        }
                        douji_choose_gui=0;
                        loadDoujiIcon();
                        return super.mouseClicked(mouseX, mouseY, button);
                    }
                }
                ++now;
            }
        }
        gongfa_choose_gui= douji_choose_gui=0;
        return super.mouseClicked(mouseX, mouseY, button);
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);

        if (douqiCap.isPresent()) {
            douqiCap.ifPresent((l) -> {
                        width = (this.minecraft.currentScreen.width - 630 / 2);//112
                        height = (this.minecraft.currentScreen.height - 380 / 2);//50
                        this.minecraft.getTextureManager().bindTexture(PlayerSkillGuiBgImg);
                        blit(matrixStack, width / 2, height / 2, 0, 0, 630 / 2, 380 / 2, 630 / 2, 380 / 2);
                        if(gongfa_choose_gui==1) {//如果选择了
                            this.minecraft.getTextureManager().bindTexture(PlayerSkillGuiGongFaChooseImg);
                            blit(matrixStack, (width+32) / 2, (height+102) / 2, 0, 0, 52 / 2, 48 / 2, 52 / 2, 48 / 2);
                        }
                        if(gongfa_choose!=-1){//如果有功法，展示功法
                            this.minecraft.getTextureManager().bindTexture(PlayerSkillGuiGongFa);
                            blit(matrixStack, (width+34) / 2, (height+104) / 2, 0,0, 48 / 2, 44/2, 48 / 2, 44 / 2);
                        }

                        //显示选择了第几组
                        drawString(matrixStack, this.minecraft.fontRenderer,""+(douji_group_choose+1), (width+462) / 2, (height+84) / 2, 0x000000);
                        if(douji_choose_gui!=0){//如果选择了斗技
                            this.minecraft.getTextureManager().bindTexture(PlayerSkillGuiDouJiChooseImg);
                            blit(matrixStack, (width+360+54*(douji_choose_gui-1)) / 2, (height+103) / 2, 0,0, 52 / 2, 48/2, 52 / 2, 48 / 2);
                        }
                        for(int i=0;i<4;++i) {//如果有斗技，展示四个斗技
                            if (douji_group[douji_group_choose * 4 + i] != -1) {
                                this.minecraft.getTextureManager().bindTexture(PlayerSkillGuiDouJi[i]);
                                blit(matrixStack, (width+362+54*i) / 2, (height+104) / 2, 0, 0, 48 / 2, 44 / 2, 48 / 2, 44 / 2);
                            }
                        }
                        int x=0,y=0,now=0;
                        for(y=0;y<8&&now<gongfa_max;++y){
                            for(x=0;x<4&&now<gongfa_max;++x){
                                this.minecraft.getTextureManager().bindTexture(PlayerSkillGuiGongFaList[y*4+x]);
                                blit(matrixStack, (width+36+x*20) / 2, (height+172+y*20) / 2, 0, 0, 16 / 2, 16 / 2, 16 / 2, 16 / 2);
                                ++now;
                            }
                        }
                        now=0;
                        for(y=0;y<8&&now<douji_max;++y){
                            for(x=0;x<4&&now<douji_max;++x){
                                this.minecraft.getTextureManager().bindTexture(PlayerSkillGuiDouJiList[y*4+x]);
                                blit(matrixStack, (width+342+x*20) / 2, (height+172+y*20) / 2, 0, 0, 16 / 2, 16 / 2, 16 / 2, 16 / 2);
                                ++now;
                            }
                        }


                            double mouse_x= mouseX*2;
                            double mouse_y= mouseY*2;
                            if(mouse_x>(width+32)&&mouse_x<(width+32+52)&&mouse_y>(height+102)&&mouse_y<(height+102+48)) {
                                if(gongfa_choose!=-1) {//存在选择的功法
                                    showGongFa(matrixStack,gongfas.get(gongfa_choose));
                                }
                            }
                            for(int i=0;i<4;++i) {//如果有斗技，展示四个斗技
                                if (mouse_x>(width+352+54*i)&&mouse_x<(width+352+54*i+48)&&mouse_y>(height+104)&&mouse_y<(height+104+44)) {
                                    if(douji_group[douji_group_choose*4+i]!=-1)
                                        showDouJi(matrixStack,doujis.get(douji_group[douji_group_choose * 4 + i]));
                                }
                            }
                now=0;
                            for(y=0;y<8&&now<gongfa_max;++y){
                                for(x=0;x<4&&now<gongfa_max;++x){
                                    if(mouse_x>(width+36+x*20)&&mouse_x<(width+36+x*20)+16&&mouse_y>(height+172+y*20)&&mouse_y<(height+172+y*20)+16) {
                                        showGongFa(matrixStack,gongfas.get(gongfa_index*IconMax+now));
                                    }
                                    ++now;
                                }
                            }
                            now=0;
                            for(y=0;y<8&&now<douji_max;++y){
                                for(x=0;x<4&&now<douji_max;++x){
                                    if(mouse_x>(width+342+x*20)&&mouse_x<(width+342+x*20)+16&&mouse_y>(height+172+y*20)&&mouse_y<(height+172+y*20)+16) {
                                        showDouJi(matrixStack,doujis.get(douji_index*IconMax+now));
                                    }
                                    ++now;
                                }
                            }
                    }
            );

        }
        super.render(matrixStack, mouseX, mouseY, partialTicks);
    }
}

